package configurator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SettingOfServer {
	private String serverType;
	private String rootPath;
	private String logPath;
	private int countThreadPool;
	private int port;
	private long timeout;
	private boolean sessionSupport;
	private ArrayList<String> subsidiariesServers;
	private int[] persents;

	public String getServerType() {
		return serverType;
	}

	@XmlElement
	public void setServerType(String serverType) {
		if (serverType.equals("simple") || serverType.equals("balancer")
				|| serverType.equals("redirection"))
			this.serverType = serverType;
		else
			this.serverType = "simple";
	}

	public int getPort() {
		return port;
	}

	@XmlElement
	public void setPort(int port) {
		if ((port > 1025) && (port < 65000))
			this.port = port;
	}

	public String getRootPath() {
		return rootPath;
	}

	@XmlElement
	public void setRootPath(String rootPath) {
		File f = new File(rootPath);
		if (f.exists() && f.isDirectory())
			this.rootPath = rootPath;
	}

	public String getLogPath() {
		return logPath;
	}

	@XmlElement
	public void setLogPath(String logPath) {
		if (logPath.indexOf(".xml") > 0)
			if (new File(logPath).exists())
				this.logPath = logPath;
	}

	public int getCountThreadPool() {
		return countThreadPool;
	}

	@XmlElement
	public void setCountThreadPool(int countThreadPool) {
		if ((countThreadPool >= 0) && (countThreadPool <= 500))
			this.countThreadPool = countThreadPool;
	}

	public long getTimeout() {
		return timeout;
	}

	@XmlElement
	public void setTimeout(long timeout) {
		if (timeout >= 0)
			this.timeout = timeout;
	}

	public boolean isSessionSupport() {
		return sessionSupport;
	}

	@XmlElement
	public void setSessionSupport(boolean sessionSupport) {
		this.sessionSupport = sessionSupport;
	}

	public ArrayList<String> getSubsidiariesServers() {
		return subsidiariesServers;
	}

	@XmlElement
	public void setSubsidiariesServers(ArrayList<String> subsidiariesServers) {
		this.subsidiariesServers = subsidiariesServers;
	}

	public void setSubsidiariesServersByIndex(int index, String sait) {
		if (this.subsidiariesServers.size() > index)
			this.subsidiariesServers.set(index, sait);
	}

	public int[] getPersents() {
		return persents;
	}

	@XmlElement
	public void setPersents(int[] persents) {
		if (persents[0] + persents[1] + persents[2] == 100)
			this.persents = persents;
	}

	// default
	public SettingOfServer() {
		try {
			if (!new File("ServerData").exists())
				new File("ServerData").mkdir();
			rootPath = "datas/";
			port = 8080;
			if (!new File("ServerData/logData.xml").exists())
				new File("ServerData/logData.xml").createNewFile();
			logPath = "ServerData/logData.xml";
		} catch (IOException e) {
			System.err.println(e);
		}
		serverType = "simple";
		countThreadPool = 15;
		timeout = 20 * 60;
		sessionSupport = false;
		persents = new int[3];
		persents[0] = 40;
		persents[1] = 25;
		persents[2] = 100 - 40 - 25;
//		subsidiariesServers = new ArrayList<String>(0);
	}

	public String toString() {
		return "RootPath - " + rootPath + "\nPort - " + port + "\nLogPath - "
				+ logPath + "\nCountThreads - " + countThreadPool
				+ "\nSessionSupport - " + sessionSupport + "\nTimeout - "
				+ timeout + "\nSubsidiariesServers(0) - "
				+ subsidiariesServers.get(0) + "\nPersents - " + persents[0]
				+ ", " + persents[1] + ", " + persents[2] + "\n";
	}
}